package com.fwtai.service.api;

import com.fwtai.api.ApiUserAddressDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 用户收货地址业务层
 * @作者 李宇鹏
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-06-28 11:07:53
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiUserAddressService {

  private final ApiUserAddressDao apiUserAddressDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ApiUserAddressService(final ApiUserAddressDao apiUserAddressDao, final ThreadPoolTaskExecutor taskExecutor){
    this.apiUserAddressDao = apiUserAddressDao;
    this.taskExecutor = taskExecutor;
  }

  public String add(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.tokenInvalid();
      final PageFormData formData = new PageFormData(request);
      final String p_user_id = "user_id";
      final String p_real_name = "real_name";
      final String p_phone = "phone";
      final String p_defaults = "defaults";
      final String p_province_id = "province_id";
      final String p_province_name = "province_name";
      final String p_city_id = "city_id";
      final String p_city_name = "city_name";
      final String p_county_id = "county_id";
      final String p_county_name = "county_name";
      final String p_address = "address";
      final String validate = ToolClient.validateField(formData,p_real_name,p_phone,p_defaults,p_province_id,p_province_name,p_city_id,p_city_name,p_county_id,p_county_name,p_address);
      if(validate != null) return validate;
      final String len_real_name = formData.getString(p_real_name);
      if(len_real_name != null && len_real_name.length() > 30){
        return ToolClient.createJsonFail("收货人姓名字数太多");
      }
      final String len_phone = formData.getString(p_phone);
      if(len_phone != null && len_phone.length() > 20){
        return ToolClient.createJsonFail("收货人联系方式字数太多");
      }
      final String len_defaults = formData.getString(p_defaults);
      if(len_defaults != null && len_defaults.length() > 1){
        return ToolClient.createJsonFail("是否为默认地址（1是，0否）字数太多");
      }
      final String len_province_id = formData.getString(p_province_id);
      if(len_province_id != null && len_province_id.length() > 20){
        return ToolClient.createJsonFail("省主键kid字数太多");
      }
      final String len_province_name = formData.getString(p_province_name);
      if(len_province_name != null && len_province_name.length() > 40){
        return ToolClient.createJsonFail("省名称字数太多");
      }
      final String len_city_id = formData.getString(p_city_id);
      if(len_city_id != null && len_city_id.length() > 20){
        return ToolClient.createJsonFail("市主键kid字数太多");
      }
      final String len_city_name = formData.getString(p_city_name);
      if(len_city_name != null && len_city_name.length() > 20){
        return ToolClient.createJsonFail("市名称字数太多");
      }
      final String len_county_id = formData.getString(p_county_id);
      if(len_county_id != null && len_county_id.length() > 20){
        return ToolClient.createJsonFail("区县kid字数太多");
      }
      final String len_county_name = formData.getString(p_county_name);
      if(len_county_name != null && len_county_name.length() > 20){
        return ToolClient.createJsonFail("区县名称字数太多");
      }
      final String len_address = formData.getString(p_address);
      if(len_address != null && len_address.length() > 128){
        return ToolClient.createJsonFail("详细地址字数太多");
      }
      final Integer v_defaults = formData.getInteger(p_defaults);
      if(v_defaults == null){
        return ToolClient.createJsonFail("是否为默认地址（1是，0否）不是有效的数字");
      }else{
        formData.put(p_defaults,v_defaults);
      }
      final Long v_province_id = formData.getLong(p_province_id);
      if(v_province_id == null){
        return ToolClient.createJsonFail("省主键kid不是有效的数据格式");
      }else{
        formData.put(p_province_id,v_province_id);
      }
      final Long v_city_id = formData.getLong(p_city_id);
      if(v_city_id == null){
        return ToolClient.createJsonFail("市主键kid不是有效的数据格式");
      }else{
        formData.put(p_city_id,v_city_id);
      }
      final Long v_county_id = formData.getLong(p_county_id);
      if(v_county_id == null){
        return ToolClient.createJsonFail("区县kid不是有效的数据格式");
      }else{
        formData.put(p_county_id,v_county_id);
      }
      formData.put(p_user_id,loginId);
      formData.put("kid",new IdBuilder().getId121L());
      return ToolClient.executeRows(apiUserAddressDao.add(formData));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null) return ToolClient.tokenInvalid();
      final PageFormData formData = new PageFormData(request);
      final String p_kid = "kid";
      final String p_real_name = "real_name";
      final String p_phone = "phone";
      final String p_defaults = "defaults";
      final String p_province_id = "province_id";
      final String p_province_name = "province_name";
      final String p_city_id = "city_id";
      final String p_city_name = "city_name";
      final String p_county_id = "county_id";
      final String p_county_name = "county_name";
      final String p_address = "address";
      final String validate = ToolClient.validateField(formData,p_kid,p_real_name,p_phone,p_defaults,p_province_id,p_province_name,p_city_id,p_city_name,p_county_id,p_county_name,p_address);
      if(validate != null) return validate;
      final String len_real_name = formData.getString(p_real_name);
      if(len_real_name != null && len_real_name.length() > 30){
        return ToolClient.createJsonFail("收货人姓名字数太多");
      }
      final String len_phone = formData.getString(p_phone);
      if(len_phone != null && len_phone.length() > 20){
        return ToolClient.createJsonFail("收货人联系方式字数太多");
      }
      final String len_defaults = formData.getString(p_defaults);
      if(len_defaults != null && len_defaults.length() > 1){
        return ToolClient.createJsonFail("是否为默认地址（1是，0否）字数太多");
      }
      final String len_province_id = formData.getString(p_province_id);
      if(len_province_id != null && len_province_id.length() > 20){
        return ToolClient.createJsonFail("省主键kid字数太多");
      }
      final String len_province_name = formData.getString(p_province_name);
      if(len_province_name != null && len_province_name.length() > 40){
        return ToolClient.createJsonFail("省名称字数太多");
      }
      final String len_city_id = formData.getString(p_city_id);
      if(len_city_id != null && len_city_id.length() > 20){
        return ToolClient.createJsonFail("市主键kid字数太多");
      }
      final String len_city_name = formData.getString(p_city_name);
      if(len_city_name != null && len_city_name.length() > 20){
        return ToolClient.createJsonFail("市名称字数太多");
      }
      final String len_county_id = formData.getString(p_county_id);
      if(len_county_id != null && len_county_id.length() > 20){
        return ToolClient.createJsonFail("区县kid字数太多");
      }
      final String len_county_name = formData.getString(p_county_name);
      if(len_county_name != null && len_county_name.length() > 20){
        return ToolClient.createJsonFail("区县名称字数太多");
      }
      final String len_address = formData.getString(p_address);
      if(len_address != null && len_address.length() > 128){
        return ToolClient.createJsonFail("详细地址字数太多");
      }
      if(len_defaults != null){
        final Integer v_defaults = formData.getInteger(p_defaults);
        if(v_defaults == null){
          return ToolClient.createJsonFail("是否为默认地址（1是，0否）不是有效的数字");
        }else{
          formData.put(p_defaults,v_defaults);
        }
      }
      final Long v_province_id = formData.getLong(p_province_id);
      if(v_province_id == null){
        return ToolClient.createJsonFail("省主键kid不是有效的数据格式");
      }else{
        formData.put(p_province_id,v_province_id);
      }
      final Long v_city_id = formData.getLong(p_city_id);
      if(v_city_id == null){
        return ToolClient.createJsonFail("市主键kid不是有效的数据格式");
      }else{
        formData.put(p_city_id,v_city_id);
      }
      final Long v_county_id = formData.getLong(p_county_id);
      if(v_county_id == null){
        return ToolClient.createJsonFail("区县kid不是有效的数据格式");
      }else{
        formData.put(p_county_id,v_county_id);
      }
      final long kid = formData.getLong(p_kid);
      final Long v = apiUserAddressDao.queryExistById(kid);
      if(v == null){
        return ToolClient.createJsonFail("数据不存在,刷新重试");
      }
      formData.put(p_kid,kid);
      return ToolClient.executeRows(apiUserAddressDao.edit(formData));
    },taskExecutor).join();
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(apiUserAddressDao.queryById(kid));
    },taskExecutor).join();
  }

  //编辑时的文件删除可参考此方法
  public String delById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.executeRows(apiUserAddressDao.delById(kid),"操作成功","删除失败,刷新重试");
    },taskExecutor).join();
  }

  public String delByKeys(final String ids){
    return CompletableFuture.supplyAsync(()->{
      if(ids == null || ids.length() == 0){
        return ToolClient.jsonValidateField();
      }
      final long[] list = ToolString.keysToArarry(ids);
      if(list == null){
        return ToolClient.createJsonFail("请选择要删除的数据");
      }
      return ToolClient.executeRows(apiUserAddressDao.delByKeys(list),"操作成功","数据不存在,刷新重试");
    },taskExecutor).join();
  }

  /**适用于api接口,仅含数据不含总记录数总条数,前端仅做返回的数据是否小于每页大小*/
  public String listData(final HttpServletRequest request){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null) return ToolClient.jsonValidateField();
      formData.put("userId",loginId);
      return ToolClient.jsonPage(apiUserAddressDao.getListData(formData));
    },taskExecutor).join();
  }

  public String getDefaultAddress(){
    final Long loginId = LocalUserId.getLong();
    return CompletableFuture.supplyAsync(()->{
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      return ToolClient.queryJson(apiUserAddressDao.getDefaultAddress(loginId));
    },taskExecutor).join();
  }
}